package net.starwifi.duobao.service.impl;

import net.starwifi.duobao.entity.DBCode;
import net.starwifi.duobao.entity.DBOrder;
import net.starwifi.duobao.entity.DBTimes;
import net.starwifi.duobao.repository.DBCodeRepository;
import net.starwifi.duobao.service.BaseServiceImpl;
import net.starwifi.duobao.service.DBCodeService;
import net.starwifi.duobao.service.DBTimesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2015/11/20.
 */
@Service
public class DBCodeServiceImpl extends BaseServiceImpl<DBCode,Long> implements DBCodeService {

    @Autowired
    DBCodeRepository repository;

    @Autowired
    DBTimesService dbTimesService;

    /** 生成夺宝*/
    @Override
    public void createDBCode(DBTimes dbTimes, int count, Long orderId) {

    }

    @Override
    public DBCode findByTimesNoAndDbCode(String timesNo, Integer dbCode) {
        return repository.findByTimesNoAndDbCode(timesNo,dbCode);
    }

    @Override
    @Transactional
    public String createCodeList(DBOrder dbOrder) {
        List<String> codeList=dbTimesService.getTimesCodeList(dbOrder.getTimesNo(),dbOrder.getQuantity());
        String codeInfo="";
        for(String code:codeList){
            DBCode dbCode=new DBCode();
            dbCode.setTimesNo(dbOrder.getTimesNo());
            dbCode.setDbOrder(dbOrder);
            Integer lastCode=10000000+Integer.parseInt(code);
            dbCode.setDbCode(lastCode);
            codeInfo=codeInfo+""+lastCode+",";
            save(dbCode);
        }
        return codeInfo;
    }
}
